package handling.cashshop.handler;

import client.MapleCharacter;
import client.MapleCharacterUtil;
import client.MapleClient;
import client.inventory.Equip;
import client.inventory.Item;
import client.inventory.MapleInventoryIdentifier;
import client.inventory.MapleInventoryType;
import constants.GameConstants;
import handling.channel.ChannelServer;
import handling.world.World;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import server.AutobanManager;
import server.CashItemFactory;
import server.CashItemInfo;
import server.CashShop;
import server.MapleCarnivalChallenge;
import server.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import server.ServerProperties;
import tools.FileoutputUtil;
import tools.MaplePacketCreator;
import tools.Triple;
import tools.data.LittleEndianAccessor;
import tools.packet.MTSCSPacket;

public class CouponCodeHandler {

    private static Logger log = Logger.getLogger(BuyCashItemHandler.class);

    public static void handlePacket(LittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
        String toPlayer = slea.readMapleAsciiString();
        String 赠送人消息 = null;
        String code = slea.readMapleAsciiString();
        boolean 赠送礼物给他人 = false;
        if (slea.available() >= 2L) {
            System.out.print(new StringBuilder().append("玩家  ").append(chr.getName()).append(" 使用奖卷赠送礼物给 ").append(toPlayer).toString());
            赠送人消息 = slea.readMapleAsciiString();
            赠送礼物给他人 = true;
        }

        if (code.length() <= 0) {
            c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
            return;
        }
        Triple info = null;
        try {
            info = MapleCharacterUtil.getNXCodeInfo(code);
        } catch (SQLException e) {
            System.out.print(new StringBuilder().append("错误 getNXCodeInfo").append(e).toString());
        }
        if ((info != null) && (((Boolean) info.left).booleanValue())) {
            int type = ((Integer) info.mid).intValue();
            int item = ((Integer) info.right).intValue();
            try {
                MapleCharacterUtil.setNXCodeUsed(chr.getName(), code);
            } catch (SQLException e) {
                System.out.print(new StringBuilder().append("错误 setNXCodeUsed").append(e).toString());
            }
            if (ServerProperties.IsLvkejian()) {
                String SendTo = new StringBuilder().append("\r\n玩家：").append(chr.getName()).append(" 使用奖卷序号：").append(code).append("\r\n赠送人：").append(toPlayer).append(!"".equals(toPlayer) ? new StringBuilder().append("   信息：").append(赠送人消息).toString() : "").append("\r\n").append("").append("奖卷类型：").append(type).append("   item = ").append(item).toString();

                System.out.print(SendTo);
                FileoutputUtil.packetLog("log\\商城奖卷数据跟踪.log", SendTo);
            }
            Map itemz = new HashMap();
            CashShop cs = chr.getCashInventory();

            int mesos = 0;
            switch (type) {
                case 0:
                    c.getPlayer().set神之子_武器点数(c.getPlayer().get神之子_武器点数() + item);
                    c.getPlayer().dropMessage(1, new StringBuilder().append("使用兑换卷成功兑换到 WP ").append(item).append(" 点！！").toString());
                    break;
                case 1:
                case 2:
                    int 赠送的贡献点 = 0;
                    if (type == 1);
                    c.getPlayer().modifyCSPoints(type, item, false);
                    c.getPlayer().addMaplewing("maple", 赠送的贡献点);
                    c.getPlayer().dropMessage(1, new StringBuilder().append("使用兑换卷成功兑换到 ").append(type == 1 ? "点卷" : "抵用卷").append("：").append(item).append(" 点！！\r\n同时获得贡献 ").append(赠送的贡献点).append(" 点！！！").toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append(chr.getName()).append(" 玩家，使用兑换卷成功兑换到 ").append(type == 1 ? "点卷" : "抵用卷").append("：").append(item).append(" 点！同时获得贡献 ").append(赠送的贡献点).append(" 点！！！").toString()));
                    break;
                case 3:
                    MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

                    boolean isCahsitem = false;
                    // CashItemInfo itez;
                    CashItemInfo itez;
                    if (ii.isCash(item)) {
                        isCahsitem = true;
                        itez = CashItemFactory.getInstance().getItemForItemId(item);
                    } else {
                        itez = CashItemFactory.getInstance().getItem(item);
                    }
                    System.out.print(new StringBuilder().append("isCahsitem = ").append(isCahsitem).toString());
                    if (!赠送礼物给他人) {
                        if ((itez == null) && (!isCahsitem)) {
                            if (!ii.itemExists(item)) {
                                c.getPlayer().dropMessage(1, new StringBuilder().append(item).append(" 这个道具不存在.").toString());
                                c.getSession().write(MTSCSPacket.商城_错误提示(0));
                                c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
                                return;
                            }
                            // Item items;
                            Item items;
                            if (GameConstants.getInventoryType(item) == MapleInventoryType.EQUIP) /* 108 */ {
                                items = ii.randomizeStats((Equip) ii.getEquipById(item));
                            } else {
                                items = new Item(item, (short) 0, (short) 1, !c.getPlayer().isSuperGM() ? 1 : (short) 0);
                            }
                            items.setGMLog(new StringBuilder().append(c.getPlayer().getName()).append(" 领取礼物").toString());
                            if (chr != null) {
                                MapleInventoryManipulator.addbyItem(chr.getClient(), items);
                            } else {
                                MapleInventoryManipulator.addbyItem(c, items);
                            }

                            c.getSession().write(MTSCSPacket.领取普通物品反馈(item, 1, "", false));
                        } else {
                            if (cs.getItemsSize() >= 100) {
                                chr.dropMessage(1, "保管箱已满");
                                c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
                                return;
                            }
                            Item itema = cs.toItem(itez);
                            byte slot = MapleInventoryManipulator.addId(c, itez.getId(), (short) 1, "", new StringBuilder().append("Cash shop: coupon code on ").append(FileoutputUtil.CurrentReadable_Date()).toString());
                            if (slot <= -1) {
                                c.getSession().write(MTSCSPacket.商城_错误提示(0));
                                return;
                            }
                            if ((itema != null) && (itema.getUniqueId() > 0) && (itema.getItemId() == itez.getId()) && (itema.getQuantity() == itez.getCount())) {
                                itemz.put(Integer.valueOf(item), itema);
                                if (ii.isCash(itema.getItemId())) {
                                    cs.addToInventory(itema);
                                    c.getSession().write(MaplePacketCreator.显示背包抵用券(chr));
                                    c.getSession().write(MTSCSPacket.领取现金物品反馈(itemz, mesos, toPlayer, false, c));
                                } else {
                                    log.info(new StringBuilder().append("[作弊] ").append(chr.getName()).append(" 商城非法领取道具.道具: ").append(itema.getItemId()).append(" - ").append(ii.getName(itema.getItemId())).toString());
                                    AutobanManager.getInstance().autoban(chr.getClient(), "商城非法领取道具.");
                                }
                            } else {
                                chr.dropMessage(1, "未知错误 ---- 1 ");
                            }

                        }

                    } else if ((itez == null) && (!isCahsitem)) {
                        if (!ii.itemExists(item)) {
                            c.getPlayer().dropMessage(1, new StringBuilder().append(item).append(" 这个道具不存在.").toString());
                            c.getSession().write(MTSCSPacket.商城_错误提示(0));
                            c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
                            return;
                        }
                        // Item items;
                        Item items;
                        if (GameConstants.getInventoryType(item) == MapleInventoryType.EQUIP) /* 163 */ {
                            items = ii.randomizeStats((Equip) ii.getEquipById(item));
                        } else {
                            items = new Item(item, (short) 0, (short) 1, !c.getPlayer().isSuperGM() ? 1 : (short) 0);
                        }
                        items.setGMLog(new StringBuilder().append(c.getPlayer().getName()).append(" 领取礼物").toString());
                        if (chr != null) {
                            MapleInventoryManipulator.addbyItem(chr.getClient(), items);
                        } else {
                            MapleInventoryManipulator.addbyItem(c, items);
                        }

                        c.getSession().write(MTSCSPacket.领取普通物品反馈(item, 1, toPlayer, true));
                    } else {
                        info = MapleCharacterUtil.getInfoByName(chr.getName(), chr.getWorld());
                        Item itema = cs.toItem(itez);
                        if ((info == null) || (((Integer) info.getLeft()).intValue() <= 0)) {
                            c.getSession().write(MTSCSPacket.商城_错误提示(7));
                        } else if ((((Integer) info.getLeft()).intValue() == chr.getId()) || (((Integer) info.getMid()).intValue() == c.getAccID())) {
                            c.getSession().write(MTSCSPacket.商城_错误提示(6));
                        } else if (!itez.genderEquals(((Integer) info.getRight()).intValue())) {
                            c.getSession().write(MTSCSPacket.商城_错误提示(8));
                        } else {
                            for (int i : GameConstants.cashBlock) {
                                if (itez.getId() == i) {
                                    chr.dropMessage(1, GameConstants.getCashBlockedMsg(itez.getId()));
                                    c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
                                    return;
                                }
                            }
                            if (!ii.isCash(itez.getId())) {
                                log.info(new StringBuilder().append("[作弊] ").append(chr.getName()).append(" 商城非法购买礼物道具.道具: ").append(itez.getId()).append(" - ").append(ii.getName(itez.getId())).toString());
                                chr.dropMessage(1, "购买商城礼物道具出现错误.");
                                c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
                                return;
                            }
                            itemz.put(Integer.valueOf(item), itema);
                            c.getSession().write(MaplePacketCreator.显示背包抵用券(chr));
                            chr.getCashInventory().gift(((Integer) info.getLeft()).intValue(), chr.getName(), 赠送人消息, itez.getSN(), MapleInventoryIdentifier.getInstance_for_maplewing());
                            c.getSession().write(MTSCSPacket.领取现金物品反馈(itemz, mesos, toPlayer, true, c));
                            chr.sendNote(chr.getName(), new StringBuilder().append(chr.getName()).append(" 您已收到").append(chr.getName()).append("送给您的礼物，请进入现金商城查看！").toString());
                            MapleCharacter receiver = ChannelServer.getCharacterByName(chr.getName());
                            if (receiver != null) {
                                receiver.showNote();
                            }
                        }
                    }

                    break;
                case 4:
                    chr.gainMeso(item, false);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得金币：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append(chr.getName()).append(" 玩家，使用兑换卷获得金币：").append(item).toString()));
                    break;
                case 5:
                    chr.addMaplewing("mapley", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得活跃点：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得活跃点：").append(item).toString()));
                    break;
                case 6:
                    chr.addMaplewing("wmose", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得万级金币：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得万级金币：").append(item).toString()));
                    break;
                case 7:
                    chr.addMaplewing("emose", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得亿级金币：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得亿级金币：").append(item).toString()));
                    break;
                case 8:
                    chr.addMaplewing("mapleb", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得枫叶点：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得枫叶点：").append(item).toString()));
                    break;
                case 9:
                    chr.addMaplewing("maplez", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得推荐点：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得推荐点：").append(item).toString()));
                    break;
                case 10:
                    chr.addMaplewing("maple", item);
                    chr.dropMessage(1, new StringBuilder().append("使用兑换卷获得贡献点：").append(item).toString());
                    World.Broadcast.broadcastMessage(MaplePacketCreator.serverNotice(6, new StringBuilder().append("[在线兑换] ").append(chr.getName()).append(" 玩家，使用兑换卷获得贡献点：").append(item).toString()));
            }

            c.getSession().write(MTSCSPacket.商城_刷新点卷(chr));
        } else {
            c.getSession().write(MTSCSPacket.商城_错误提示(info == null ? 14 : 16));
        }
    }
}
